package com.hiultra.archive.service.impl;

import com.hiultra.archive.domain.ArcArchive;
import com.hiultra.archive.domain.ArcBox;
import com.hiultra.archive.mapper.ArcArchiveMapper;
import com.hiultra.archive.mapper.ArcBoxMapper;
import com.hiultra.archive.service.IArcArchiveService;
import com.hiultra.common.constant.UserConstants;
import com.hiultra.common.exception.ServiceException;
import com.hiultra.common.utils.DateUtils;
import com.hiultra.common.utils.StringUtils;
import com.hiultra.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 档案Service业务层处理
 *
 * @author ls
 * @date 2022-07-30
 */
@Service
public class ArcArchiveServiceImpl implements IArcArchiveService {
    @Autowired
    private ArcArchiveMapper arcArchiveMapper;
    private static final Logger log = LoggerFactory.getLogger(ArcDossierServiceImpl.class);
    @Autowired
    protected Validator validator;
    @Autowired
    private ArcBoxMapper arcBoxMapper;

    /**
     * 查询档案
     *
     * @param archiveId 档案主键
     * @return 档案
     */
    @Override
    public ArcArchive selectArcArchiveByArchiveId(Long archiveId) {
        return arcArchiveMapper.selectArcArchiveByArchiveId(archiveId);
    }

    /**
     * 查询档案列表
     *
     * @param arcArchive 档案
     * @return 档案
     */
    @Override
    public List<ArcArchive> selectArcArchiveList(ArcArchive arcArchive) {
        return arcArchiveMapper.selectArcArchiveList(arcArchive);
    }

    /**
     * 新增档案
     *
     * @param arcArchive 档案
     * @return 结果
     */
    @Override
    public int insertArcArchive(ArcArchive arcArchive) {
        //查询最大件号
        long maxArchiveCode = arcArchiveMapper.selectMaxArchiveCode();
        arcArchive.setPieceCode(maxArchiveCode + 1);
//        if (null != arcArchive.getBoxId()) {
//            //根据盒号位置更新档案位置
//            ArcBox arcBox = arcBoxMapper.selectArcBoxByBoxId(arcArchive.getBoxId());
//            arcArchive.setShelvesId(arcBox.getShelvesId());
//            arcArchive.setShelvesCode(arcBox.getShelvesCode());
//            arcArchive.setShelvesName(arcBox.getShelvesName());
//            arcArchive.setShelvesLayerGrid(arcBox.getShelvesLayerGrid());
//            arcArchive.setShelvesLayerGridName(arcBox.getShelvesLayerGridName());
//        }
        return arcArchiveMapper.insertArcArchive(arcArchive);
    }

    /**
     * 修改档案
     *
     * @param arcArchive 档案
     * @return 结果
     */
    @Override
    public int updateArcArchive(ArcArchive arcArchive) {
        arcArchive.setUpdateTime(DateUtils.getNowDate());
        if (null != arcArchive.getBoxId()) {
            //根据盒号位置更新档案位置
            ArcBox arcBox = arcBoxMapper.selectArcBoxByBoxId(arcArchive.getBoxId().toString());
//            arcArchive.setShelvesId(arcBox.getShelvesId());
//            arcArchive.setShelvesCode(arcBox.getShelvesCode());
//            arcArchive.setShelvesName(arcBox.getShelvesName());
//            arcArchive.setShelvesLayerGrid(arcBox.getShelvesLayerGrid());
//            arcArchive.setShelvesLayerGridName(arcBox.getShelvesLayerGridName());
        }
        return arcArchiveMapper.updateArcArchive(arcArchive);
    }

    /**
     * 批量删除档案
     *
     * @param archiveIds 需要删除的档案主键
     * @return 结果
     */
    @Override
    public int deleteArcArchiveByArchiveIds(Long[] archiveIds) {
        return arcArchiveMapper.deleteArcArchiveByArchiveIds(archiveIds);
    }

    /**
     * 删除档案信息
     *
     * @param archiveId 档案主键
     * @return 结果
     */
    @Override
    public int deleteArcArchiveByArchiveId(Long archiveId) {
        return arcArchiveMapper.deleteArcArchiveByArchiveId(archiveId);
    }

    /**
     * 校验档案档号称是否唯一
     *
     * @param arcArchive 档案
     * @return 结果
     */
    @Override
    public String checkArchiveCodeUnique(ArcArchive arcArchive) {
        Long archiveId = StringUtils.isNull(arcArchive.getArchiveId()) ? -1L : arcArchive.getArchiveId();
        ArcArchive info = arcArchiveMapper.checkArchiveCodeUnique(arcArchive);
        if (StringUtils.isNotNull(info) && info.getArchiveId().longValue() != archiveId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 导入档案数据
     *
     * @param arcArchiveList  档案数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importArchive(List<ArcArchive> arcArchiveList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(arcArchiveList) || arcArchiveList.size() == 0) {
            throw new ServiceException("导入档案数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ArcArchive archive : arcArchiveList) {
            //查数据库当前最大件号
            long maxArchiveCode = arcArchiveMapper.selectMaxArchiveCode();
            archive.setPieceCode(maxArchiveCode + 1);
            try {
                // 验证是否存在档案盒信息
                ArcArchive a = arcArchiveMapper.selectArcArchiveByArchiveCode(archive.getArchiveCode());
                if (StringUtils.isNull(a)) {
                    BeanValidators.validateWithException(validator, archive);
                    archive.setCreateBy(operName);
                    this.insertArcArchive(archive);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、档案" + archive.getArchiveCode() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, archive);
                    archive.setUpdateBy(operName);
                    this.updateArcArchive(archive);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、档案" + archive.getArchiveCode() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、档案" + archive.getArchiveCode() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、档案" + archive.getArchiveCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
